<?php
class Author {
	private $author_id;
	private $author;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $website;
	private $newsletter;
	private $address_id;
	
  	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
				
		if (isset($this->session->data['author_id'])) { 
			$author_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_author WHERE author_id = '" . (int)$this->session->data['author_id'] . "' AND status = '1'");
			
			if ($author_query->num_rows) {
				$this->author_id = $author_query->row['author_id'];
				$this->author = $author_query->row['author'];
				$this->firstname = $author_query->row['firstname'];
				$this->lastname = $author_query->row['lastname'];
				$this->email = $author_query->row['email'];
				$this->telephone = $author_query->row['telephone'];
				$this->website = $author_query->row['website'];
				$this->newsletter = $author_query->row['newsletter'];
							
      			$this->db->query("UPDATE " . DB_PREFIX . "news_author SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE author_id = '" . (int)$this->author_id . "'");
			
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_author_ip WHERE author_id = '" . (int)$this->session->data['author_id'] . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'");
				
				if (!$query->num_rows) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "news_author_ip SET author_id = '" . (int)$this->session->data['author_id'] . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()");
				}
			} else {
				$this->logout();
			}
  		}
	}
		
  	public function login($email, $password, $override = false) {
		if ($override) {
			$author_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_author where LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND status = '1'");
		} else {
			$author_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_author WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1' AND approved = '1'");
		}
		
		if ($author_query->num_rows) {
			$this->session->data['author_id'] = $author_query->row['author_id'];	
		  									
			$this->author_id = $author_query->row['author_id'];
			$this->author = $author_query->row['author'];
			$this->firstname = $author_query->row['firstname'];
			$this->lastname = $author_query->row['lastname'];
			$this->email = $author_query->row['email'];
			$this->telephone = $author_query->row['telephone'];
			$this->website = $author_query->row['website'];
			$this->newsletter = $author_query->row['newsletter'];
          	
			$this->db->query("UPDATE " . DB_PREFIX . "news_author SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE author_id = '" . (int)$this->author_id . "'");
			
	  		return true;
    	} else {
      		return false;
    	}
  	}
  	
	public function logout() {		
		unset($this->session->data['author_id']);

		$this->author_id = '';
		$this->author = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->website = '';
		$this->newsletter = '';
  	}
  
  	public function isLogged() {
    	return $this->author_id;
  	}

  	public function getId() {
    	return $this->author_id;
  	}    
  	public function getAuthor() {
		return $this->author;
  	}  
  	public function getFirstName() {
		return $this->firstname;
  	}
  
  	public function getLastName() {
		return $this->lastname;
  	}
  
  	public function getEmail() {
		return $this->email;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  
  	public function getWebsite() {
		return $this->website;
  	}
	
  	public function getNewsletter() {
		return $this->newsletter;	
  	}

}
?>